package com.ciaojian.core.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ciaojian.core.enums.business.CouponCodeTakeRecordTypeEnum;
import com.ciaojian.core.mapper.MarketingShareMapper;
import com.ciaojian.core.model.MarketingShare;
import com.ciaojian.core.model.vo.ShareRecordVo;
import com.ciaojian.core.util.Result;

import javax.annotation.Resource;
import java.util.List;

public abstract class MarketingShareService extends ServiceImpl<MarketingShareMapper, MarketingShare>{

    @Resource
    private MarketingShareService marketingShareService;

    @Resource
    private ShareRecordService shareRecordService;

    @Resource
    private CouponCodeService couponCodeService;

    public Result sendCoupon(Integer inviterId, Integer shareId, Integer mId){
        if(inviterId != null){
            MarketingShare m = marketingShareService.getById(mId);
            Integer limit = m.getLimitNum();//限制人数
            Integer shareCount = shareRecordService.countByInvitedMarketing(inviterId,mId);
            if(shareCount >= limit){
                return Result.error("分享人数到达上限");
            }
            if(inviterId.equals(shareId)){
                return Result.error("不能分享给自己");
            }
            if(!m.getInviterCoupon()){//券
                couponCodeService.takeCodeMS(inviterId,m.getInviterCouponId(),m.getId(),shareId,m.getShareCouponId(), CouponCodeTakeRecordTypeEnum.MARKETING_SHARE_IN);
            }else {//券包

            }
        }
        return Result.ok(200,"领取成功");
    }

    public List<ShareRecordVo> shareCount(Integer inviterId, Integer mId){
        return baseMapper.shareCount(inviterId,mId);
    }
}
